package pkg.andru.core;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;

public class CommonFragment extends Fragment {

    public static boolean DEBUG_LOG = true;

    protected CommonFragmentManager mCommonFragmentManager;

    protected String mFragmentTag;

    final String LOG_TAG = getClass().getSimpleName();

    CommonFragmentActivity mActivity;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof CommonFragmentActivity) {
            mActivity = (CommonFragmentActivity) activity;
            mCommonFragmentManager = mActivity.getCommonFragmentManager();
        }
        if (DEBUG_LOG) {
            Log.i(LOG_TAG, "onAttach...");
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (DEBUG_LOG) {
            Log.i(LOG_TAG, "onCreate...");
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (DEBUG_LOG) {
            Log.i(LOG_TAG, "onStart...");
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (DEBUG_LOG) {
            Log.i(LOG_TAG, "onResume...");
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (DEBUG_LOG) {
            Log.i(LOG_TAG, "onPause...");
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (DEBUG_LOG) {
            Log.i(LOG_TAG, "onStop...");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (DEBUG_LOG) {
            Log.i(LOG_TAG, "onDestroyView...");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (DEBUG_LOG) {
            Log.i(LOG_TAG, "onDestroy...");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mActivity = null;
        mCommonFragmentManager = null;

        if (DEBUG_LOG) {
            Log.i(LOG_TAG, "onDetach...");
        }
    }

    public void processOnClick(View view) {

    }

    /**
     * the back pressed will use the {@link Activity#onBackPressed()} method,
     * this method supply the function the user don't use the back pressed, and
     * need back to previous step
     */
    public void backPressed() {
        if (mActivity != null) {
            mActivity.onBackPressed();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // dont invoke the super method to avoid a state error bug
        // TODO Auto-generated method stub
        // super.onSaveInstanceState(outState);
    }

    public void setFragmentTag(String tag) {
        mFragmentTag = tag;
    }

    public String getFragmentTag() {
        return mFragmentTag;
    }
}
